package cn.flying.cloud.system.dict.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.dict.entity.SysDictType;
import cn.flying.cloud.system.dict.service.SysDictDataService;
import cn.flying.cloud.system.dict.service.SysDictTypeService;

/**
 * 枚举字段类型表 前端控制器
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysDictTypeService sysDictTypeService;
    @Resource
    private SysDictDataService sysDictDataService;

    /**
     * 分页查询枚举字段类型表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    public PageResponse<SysDictType> page(@RequestBody PageRequest<SysDictType> request) {
        return sysDictTypeService.getPageList(request);
    }

    /**
     * 列表查询枚举字段类型表数据
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody SysDictType sysDictType) {
        List<SysDictType> list = sysDictTypeService.getList(sysDictType);
        return Rt.success(list);
    }

    /**
     * 查询枚举字段类型表详情数据
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(SysDictType sysDictType) {
        return Rt.success(sysDictTypeService.getOneObj(sysDictType));
    }

    /**
     * 根据主键ID查询枚举字段类型表详情数据
     *
     * @param id
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        SysDictType sysDictType = sysDictTypeService.getById(id);
        return Rt.success(sysDictType);
    }

    /**
     * 保存枚举字段类型表数据
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody SysDictType sysDictType) {
        if (Objects.isNull(sysDictType.getId())) {
            // 新增
            sysDictTypeService.createObj(sysDictType);
        } else {
            // 更新
            sysDictTypeService.updateObj(sysDictType);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除枚举字段类型表数据
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(SysDictType sysDictType) {
        sysDictTypeService.deleteObj(sysDictType);
        return Rt.success("删除成功！");
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        sysDictTypeService.deleteById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Rt<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        sysDictTypeService.importExcel(file);
        return Rt.success("导入成功！");
    }

    /**
     * 刷新缓存
     *
     * @return
     */
    @PostMapping("refresh")
    public Rt<?> refresh() {
        sysDictDataService.refresh();
        return Rt.success("刷新成功！");
    }
}
